﻿using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;

namespace XEvolve.Presentation.Internal
{
    class WindowAdapter
    {
        private readonly Window target;

        [DllImport("user32")]
        internal static extern int SetForegroundWindow(IntPtr hwnd);


        public WindowAdapter(ViewKey key, Window target, ViewManager viewManager)
        {
            this.target = target;
            this.target.Closed += delegate { viewManager.CloseView(key); };
        }

        public void Show(object parameter)
        {
            target.RaiseEvent(new InitializeViewEventArgs(parameter));
            target.Show();
        }

        public void BringToFront()
        {
            SetForegroundWindow(new WindowInteropHelper(target).Handle);
            target.Focus();
        }

        public void Close()
        {
            target.Close();
        }
    }
}